export default function (postArr, getArr) {
    let $ = layui.jquery
    let posturl = '';
    let sendArr = [];
    var config = {
        useCdnDomain: false,
        region: null,
    };

    function convertBase64ToBlob(base64) {
        const base64Arr = base64.split(',');
        let imgtype = '';
        let base64String = '';
        if (base64Arr.length > 1) {
            base64String = base64Arr[1];
            imgtype = base64Arr[0].substring(base64Arr[0].indexOf(':') + 1, base64Arr[0].indexOf(';'));
        }
        const bytes = atob(base64String);
        const bytesCode = new ArrayBuffer(bytes.length);
        const byteArray = new Uint8Array(bytesCode);
        for (let i = 0; i < bytes.length; i++) {
            byteArray[i] = bytes.charCodeAt(i);
        }
        return new Blob([bytesCode], {type: imgtype});
    }

    function uploadImg(token, index) {
        return new Promise((resolve, reject) => {
            const putExtra = {};
            const observer = {
                next() {

                },
                error() {
                },
                complete(res) {
                    posturl = 'http://crm.blingblingstar.com/' + res.key;
                    resolve(posturl)
                },
            };
            let postfile = convertBase64ToBlob(postArr[index].postfile);
            let postname = postArr[index].postname
            const observable = qiniu.upload(postfile, postname, token, putExtra, config);
            observable.subscribe(observer);
        })
    }

    function getUrl() {
        return new Promise((resolve, reject) => {
            $.ajax({
                type: "GET",
                url: "/ImageUpload",
                data: {},
                success: (res) => {
                    postArr.forEach((item, i) => {
                        uploadImg(res, i).then((res) => {
                            sendArr.push(res);
                            console.log(sendArr)
                            if (sendArr.length === postArr.length) {
                                console.log(sendArr)
                                resolve(sendArr)
                            }
                        })
                    });
                    // console.log(sendArr);
                    // resolve(sendArr)
                },
                error() {

                }
            });
        })
    }

    return getUrl();
}